import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';
import { stringFormat } from '/@/utils/common/compUtils';

enum Api {
  list = '/api-iot/iot/modbus/list',
  save = '/api-iot/iot/modbus/saveOrUpdate',
  edit = '/api-iot/iot/modbus/saveOrUpdate',
  delete = '/api-iot/iot/modbus/delete',
  deleteBatch = '/api-iot/iot/modbus/deleteBatch',
  queryById = '/api-iot/iot/modbus/queryById',
  connect = '/api-iot/iot/modbus/connect/{0}',
}

/**
 * 列表接口-分页
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params });

/**
 * 删除设备
 * @param params 
 * @param handleSuccess 
 * @returns 
 */
export const deleteModbus = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};


/**
 * 批量删除设备
 * @param params
 */
export const batchDeleteModbus = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 保存或者更新设备
 * @param params
 */
export const saveOrUpdateModbus = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

/**
 * 查询设备-ID
 * @param params
 */
export const getModbusById = (params) => {
  return defHttp.get({ url: Api.queryById, params });
};

/**
 * 查询设备-端口打开或关闭
 * @param params
 */
 export const modbusConnect = (params) => {
  const url = stringFormat(Api.connect, params.status); 
  return defHttp.post({ url: url, params }, { isTransformResponse: false });
};
